/** { "style":{ "navigationBarTitleText":"登录", "navigationStyle": "custom", } } */

<template>
	<view class="account-page">
		<u-navbar title="登录" leftIcon="home" @leftClick="leftClick"></u-navbar>
		<view class="content">
			<view class="image">
				<!-- <image src="/static/image/logo.png" mode=""></image> -->
			</view>
			<view class="title">欢迎登录新君涛验车</view>
			<view class="form">
				<view class="item">
					<view class="zhanghap">账号</view>
					<view class="input">
						<!-- <u--input type="text" height="100%" border="none" v-model="formData.phone" placeholder="请输入手机号" /> -->
						<input
							type="text"
							v-model="formData.phone"
							placeholder="请输入手机号"
							:placeholder-style="{ color: '#999' }"
						/>
					</view>
				</view>

				<view class="item">
					<view class="zhanghap">密码</view>
					<view class="input">
						<input
							type="text"
							password="true"
							v-model="formData.password"
							placeholder="请输入密码"
							:placeholder-style="{ color: '#999' }"
						/>
						<!-- <u--input type="password" border="none" v-model="formData.password" placeholder="请输入密码" /> -->
					</view>
				</view>
			</view>

			<view class="btn" @click="onLogin">登录</view>

			<view class="hint1 hint-login" @click="jumpLogin">手机号一键登录</view>
		</view>
	</view>
</template>

<script>
// import {
// 	passwordLogin,
// 	getUserInfo
// } from '@/apis/home.js';
export default {
	data() {
		return {
			type: '',
			formData: {
				phone: '',
				password: '',
				code: ''
			}
		};
	},
	onLoad(options) {
		this.type = options.type;
	},
	methods: {
		leftClick() {
			uni.switchTab({
				url: '/pages/index/index'
			});
		},
		async onLogin() {
			if (!this.formData.phone) {
				uni.showToast({
					title: '请输入手机号',
					icon: 'none'
				});
				return;
			}
			if (!/^1\d{10}$/.test(this.formData.phone)) {
				uni.showToast({
					title: '请输入11位手机号',
					icon: 'none'
				});
				return;
			}
			if (!this.formData.password) {
				uni.showToast({
					title: '请输入请输入密码',
					icon: 'none'
				});
				return;
			}
			let _this = this;
			let code = '';
			// #ifdef MP-ALIPAY
			code = await new Promise((ok) =>
				uni.login({
					provider: 'alipay',
					success(res) {
						ok(res.code);
					}
				})
			);
			// #endif

			// #ifdef MP-WEIXIN
			code = await new Promise((ok) =>
				uni.login({
					provider: 'weixin',
					success(res) {
						ok(res.code);
					}
				})
			);
			// #endif

			let invite_id = '';
			if (uni.getStorageSync('fid')) {
				invite_id = uni.getStorageSync('fid');
			}
			let res2 = await $post('/index/base/account_pwd/pwd_login', {
				phone: this.formData.phone, //手机号
				password: this.formData.password, //密码
				invite_id, //邀请人id (可选)
				code
			});
			uni.setStorageSync('token', `"${res2.token}"`);
			// const res3 = await getUserInfo()
			const res3 = await $post('/index/base/user/info');
			uni.setStorageSync('userId', res3.id);
			try {
				uni.navigateBack({
					delta: 1
				});
			} catch (e) {
				uni.switchTab({
					url: '/pages/index/index'
				});
			}

			return;
			uni.setStorageSync('isBtn', 1);

			if (_this.type == 1) {
				const fromPage = uni.getStorageSync('fromPage');
				if (fromPage == 'my') {
					uni.switchTab({
						url: '/pages/my/my'
					});
				} else if (fromPage == 'card') {
					uni.switchTab({
						url: '/pages/card/card'
					});
				} else {
					uni.navigateBack({
						delta: 1
					});
				}
				uni.removeStorageSync('fromPage');
			} else {
				uni.switchTab({
					url: '/pages/index/index'
				});
			}
		},
		jumpLogin() {
			uni.redirectTo({
				url: '/pages/login/login?type=' + this.type
			});
		}
	}
};
</script>
<style lang="scss" scoped>
input {
	&::placeholder {
		color: #999;
	}
}

.content {
	width: 100%;
	height: 100vh;
	padding-top: 200rpx;
	background-color: #fff;

	.image {
		width: 100%;
		text-align: center;

		image {
			width: 158rpx;
			height: 68rpx;
		}
	}

	.title {
		font-family: PingFang SC, PingFang SC;
		font-weight: 800;
		font-size: 34rpx;
		color: #333333;
		width: 100%;
		text-align: center;
		padding-top: 74rpx;
	}

	.form {
		padding-top: 40rpx;
	}

	.item {
		width: 636rpx;
		margin: 40rpx auto 0;

		.zhanghap {
			font-size: 32rpx;
			color: #333333;
			margin-bottom: 20rpx;
		}

		.input {
			width: 100%;
			height: 80rpx;
			border-radius: 80rpx;
			border: 1px solid #999;
			line-height: 80rpx;
			padding: 0 26rpx;
			overflow: hidden;

			input {
				width: 100%;
				height: 100%;
				font-size: 28rpx;
				border: 0;
			}
		}
	}
}

.btn {
	width: 636rpx;
	height: 80rpx;
	font-size: 28rpx;
	color: #fff;
	background-color: #0f956e;
	text-align: center;
	line-height: 80rpx;
	border-radius: 80rpx;
	margin: 50rpx auto 0;
}
</style>
